package org.ucas.kgraph.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.ucas.kgraph.dao.RoleDao;
import org.ucas.kgraph.dto.*;
import org.ucas.kgraph.entity.Account;
import org.ucas.kgraph.entity.Perm;
import org.ucas.kgraph.entity.Relation;
import org.ucas.kgraph.entity.RelationType;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.entity.Role;
import org.ucas.kgraph.service.PermService;
import org.ucas.kgraph.service.RelationService;
import org.ucas.kgraph.service.RoleService;
import org.ucas.kgraph.service.SchemService;
import org.ucas.kgraph.utils.exception.BadRequestException;
import org.ucas.kgraph.utils.exception.ConflictException;
import org.ucas.kgraph.utils.exception.NotFoundException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class RoleController extends BaseController {

  @Autowired
  private RoleService roleService;
  @Autowired
  private PermService permService;
  
  
  
  @RequestMapping(value = "/roles/list",method = RequestMethod.GET)
  public ListDto<RoleDto> roles(){
	  return ListDto.of(roleService.listRoleAll(),RoleDto::of);
  }
  
  @RequestMapping(value = "/roles/{id:\\d+}/listbyId",method = RequestMethod.GET)
  public ListDto<PermDto> rolesPerms(@PathVariable Long id){
	  return ListDto.of(roleService.listRolePerms(id),PermDto::of);
  }
  
  @RequestMapping(value = "/roles",method = RequestMethod.POST)
  public CarryDto<Role> create(@RequestParam("name") String name,@RequestParam("id_list") String ids){
	  Role role = new Role();
	  List<Perm> permList = new ArrayList<>();
	  String[] id_list = ids.split(";");
	  for(int i=0;i<id_list.length;i++){
		  permList.add(permService.findById(Long.valueOf(id_list[i])));
	  }
	  role.setPermList(permList);
	  role.setName(name);
	  roleService.newRole(role);
	  return CarryDto.of(role);
  }
  
  @RequestMapping(value = "/roles/edit",method = RequestMethod.POST)
  public CarryDto<Role> edit(@RequestParam("id") Long id,@RequestParam("name") String name,@RequestParam("id_list") String ids){
	  Role role = roleService.load(id);
	  
	  List<Perm> permList = new ArrayList<>();
	  String[] id_list = ids.split(";");
	  for(int i=0;i<id_list.length;i++){
		  permList.add(permService.findById(Long.valueOf(id_list[i])));
	  }
	  role.setPermList(permList);
	 
	  if(role.getName().equals(name)){
		  roleService.save(role);
	  }else{
		  if(roleService.findByName(name).size()==0){
			  
			  role.setName(name);
			  roleService.save(role);
		  }else{
			  throw ConflictException.Role_Conflict;
		  }
	  }
	  return CarryDto.of(role);
  }
  @RequestMapping(value = "/roles/{id:\\d+}/delete",method = RequestMethod.DELETE)
  public ResponseDto edit(@PathVariable Long id){
	  Role role = roleService.load(id);
	  
	  roleService.delete(role);
	 
	  
	  return ResponseDto.OK;
  }
  
  

}
